package com.itheima.project.face.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.constant.dict.DataDictCacheConstant;
import com.itheima.project.core.enums.dict.DataDictEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.DataDictFace;
import com.itheima.project.pojo.DataDict;
import com.itheima.project.service.IDataDictService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.dict.DataDictVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @ClassName IDataDictFaceImpl.java
 * @Description 数字字典face接口类实习
 */
@Slf4j
@Component
public class DataDictFaceImpl implements DataDictFace {

    @Autowired
    IDataDictService dataDictService;

    @Override
    @Cacheable(value = DataDictCacheConstant.PAGE,key ="#pageNum+'-'+#pageSize+'-'+#dataDictVo.hashCode()")
    public Page<DataDictVo> findDataDictVoPage(DataDictVo dataDictVo, int pageNum, int pageSize) throws ProjectException {
        try {
            Page<DataDict> page = dataDictService.findDataDictVoPage(dataDictVo, pageNum, pageSize);
            Page<DataDictVo> pageVo = BeanConv.toPage(page,DataDictVo.class);
            return pageVo;
        }catch (Exception e){
            log.error("查询数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.PAGE_FAIL);
        }
    }

    @Override
    @Caching(
        evict={@CacheEvict(value = DataDictCacheConstant.PAGE,allEntries = true),
            @CacheEvict(value = DataDictCacheConstant.PARENT_KEY,key = "#dataDictVo.parentKey")},
        put={@CachePut(value = DataDictCacheConstant.DATA_KEY,key = "#dataDictVo.dataKey")})
    @Transactional
    public DataDictVo saveDataDict(DataDictVo dataDictVo) throws ProjectException {
        try {
            DataDict dataDict =BeanConv.toBean(dataDictVo,DataDict.class);
            boolean flag = dataDictService.save(dataDict);
            if (flag){
                return BeanConv.toBean(dataDict,DataDictVo.class);
            }else {
                log.error("保存数据字典列表异常!");
                throw new ProjectException(DataDictEnum.SAVE_FAIL);
            }
        }catch (Exception e){
            log.error("保存数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.SAVE_FAIL);
        }
    }

    @Override
    @Caching(
        evict={@CacheEvict(value = DataDictCacheConstant.PAGE,allEntries = true),
            @CacheEvict(value = DataDictCacheConstant.PARENT_KEY,key = "#dataDictVo.parentKey"),
            @CacheEvict(value = DataDictCacheConstant.DATA_KEY,key = "#dataDictVo.parentKey")})
    @Transactional
    public DataDictVo updateDataDict(DataDictVo dataDictVo) throws ProjectException {
        try {
            DataDict dataDict = BeanConv.toBean(dataDictVo, DataDict.class);
            boolean flag = dataDictService.updateById(dataDict);
            if (flag){
                return BeanConv.toBean(dataDict, DataDictVo.class);
            }else {
                log.error("修改数据字典列表异常!");
                throw new ProjectException(DataDictEnum.UPDATE_FAIL);
            }
        }catch (Exception e){
            log.error("修改数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.UPDATE_FAIL);
        }
    }

    @Override
    @Cacheable(value = DataDictCacheConstant.DATA_KEY,key = "#dataKey")
    public DataDictVo findDataDictVoByDataKey(String dataKey) throws ProjectException {
        try {
            DataDict dataDict = dataDictService.findDataDictVoByDataKey(dataKey);
            return BeanConv.toBean(dataDict,DataDictVo.class);
        }catch (Exception e){
            log.error("查询数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.FIND_DATADICTVO_DATAKEY);
        }

    }

    @Override
    public List<DataDictVo> findValueByDataKeys(List<String> dataKeys) throws ProjectException {
        List<DataDictVo> dataDictVos = new ArrayList<>();
        dataKeys.forEach(n->{
            dataDictVos.add(this.findDataDictVoByDataKey(n));
        });
        return dataDictVos;
    }

    @Override
    @Cacheable(value = DataDictCacheConstant.PARENT_KEY,key = "#parentKey")
    public List<DataDictVo> findDataDictVoByParentKey(String parentKey) throws ProjectException {
        try {
            List<DataDict> dataDicts = dataDictService.findDataDictByParentKey(parentKey);
            return BeanConv.toBeanList(dataDicts,DataDictVo.class);
        }catch (Exception e){
            log.error("查询数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.FIND_DATADICTVO_PARENTKEY);
        }
    }

    @Override
    public Set<String> findParentKeyAll() throws ProjectException  {
        try {
            return dataDictService.findParentKeyAll();
        }catch (Exception e){
            log.error("查询数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.FIND_PARENTKEY_ALL);
        }
    }

    @Override
    public Set<String> findDataKeyAll() throws ProjectException{
        try {
            return dataDictService.findDataKeyAll();
        }catch (Exception e){
            log.error("查询数据字典列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(DataDictEnum.FIND_DATAKEY_ALL);
        }
    }

}
